//app.js
App({
  uploadimg: function (data) {
    var that = this,
      i = data.i ? data.i : 0,
      success = data.success ? data.success : 0,
      fail = data.fail ? data.fail : 0;
    console.log('data数据' + data.path[i]);
    console.log('data数据1' + data.path[i][0]);
    console.log(i);
    const uploadTask = wx.uploadFile({
      url: data.url,
      header: {
        'content-type': 'multipart/form-data'
      },
      filePath: data.path[i],
      name: data.name, //自定义上传后的文件名如（fileData.jpg fileData.png 等等不限制后缀）
      formData: {
        imagesType: data.imagesType //自定义的上传路径方式
      },
      success: resp => {
        success++;
        var jsonData = JSON.parse(resp.data);
        that.globalData.imgResponseUrl = jsonData.data;
        if (this.imgResponseUrlReadyCallback) {
          this.imgResponseUrlReadyCallback(that.globalData.imgResponseUrl)
        }
        //typeof func == "function" && func(that.globalData.imgResponseUrl);
      },
      fail: (res) => {
        fail++;
        console.log('fail:' + i + "fail:" + fail);
      },
      complete: () => {
        console.log(i);
        i++;
        if (i == data.path.length) {
          console.log('执行完毕');
          console.log('成功：' + success + " 失败：" + fail);
        } else {
          console.log(i);
          data.i = i;
          data.success = success;
          data.fail = fail;
          that.uploadimg(data);
        }
      }
    });
    uploadTask.onProgressUpdate((res) => {
      console.log('上传进度', res.progress)
      that.globalData.progress = res.progress;
      console.log('已经上传的数据长度', res.totalBytesSent)
      console.log('预期需要上传的数据总长度', res.totalBytesExpectedToSend)
    })
  },
  uploadVideo: function (data) {
    var that = this
    wx.uploadFile({
      url: data.url,
      header: {
        'content-type': 'multipart/form-data'
      },
      filePath: data.path,
      name: data.name, //自定义上传后的文件名如（fileData.jpg fileData.png 等等不限制后缀）
      formData: {
        imagesType: data.imagesType //自定义的上传路径方式
      },
      success: resp => {
        var jsonData = JSON.parse(resp.data);
        that.globalData.imgResponseUrl = jsonData.data;
        if (this.videoResponseUrlReadyCallback) {
          this.videoResponseUrlReadyCallback(that.globalData.imgResponseUrl)
        }
        //typeof func == "function" && func(that.globalData.imgResponseUrl);
      },
      fail: (res) => {
        console.log(res);
      },
      complete: () => {
        console.log('执行完毕');
      }
    });
  },

  onLaunch: function () {
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
  },
  //下单
  wxPay: function (data) {
    var that = this;
    var total_fee = data.total_fee;
    var body = data.body;
    var openid = data.openid;
    if (total_fee > 0 && body != null && body != "" && openid != null && openid != "") {
      wx.request({
        url: that.globalData.requestURL + 'wxPay/placeAnOrder',
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          total_fee: total_fee,//费用金额
          body: body,//办理的业务
          openid: openid,//用户
        },
        success: res => {
          var prepay_id = res.data.data.prepay_id;
          if (this.prepay_idReadyCallback) {
            this.prepay_idReadyCallback(prepay_id)
          }
          that.sign(prepay_id);
        }
      })
    }
  },
  //签名
  sign: function (prepay_id) {
    var that = this;
    wx.request({
      url: that.globalData.requestURL + 'wxPay/autoGraph',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: { 'repay_id': prepay_id },
      success: res => {
        var sign = res.data.data;
        if (this.signReadyCallback) {
          this.signReadyCallback(sign)
        }
      }
    })
  },

  //微信用户登录获取微信用户的
  wxLogin: function () {
    var that = this;
    var code;
    wx.checkSession({
      success: function () {
        //session_key 未过期，并且在本生命周期一直有效
        console.log("登录状态未过期");
        wx.getUserInfo({
          withCredentials: true,
          success: res4 => {
            // 可以将 res 发送给后台解码出 unionId
            that.globalData.userInfo = res4
            // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
            // 所以此处加入 callback 以防止这种情况
            if (that.userInfoReadyCallback) {
              that.userInfoReadyCallback(that.globalData.userInfo);
            }
            var encryptedData = that.globalData.userInfo.encryptedData;
            var openid = wx.getStorageSync("openid");
            var iv = that.globalData.userInfo.iv;
            wx.request({
              url: that.globalData.requestURL + 'wxUserLog/getUseuInfo',
              data: {
                encryptedData: encryptedData,
                openid: openid,
                iv: iv
              },
              header: {
                'content-type': 'application/x-www-form-urlencoded'
              },
              success: function (res6) {
                that.globalData.userInfoData = res6.data;
                if (that.userInfoDataReadyCallback) {
                  that.userInfoDataReadyCallback(that.globalData.userInfoData);
                }
              }
            })
          },
          fail: fal => {
            wx.showModal({
              title: '拒绝授权',
              content: '部分功能将受影响',
              showCancel: false
            })
          }
        })
      },
      fail: function () {
        // session_key 已经失效，需要重新执行登录流程
        console.log("登录状态已过期");
        wx.login({
          success: function (res1) {
            code = res1.code;
            wx.request({
              url: that.globalData.requestURL + 'wxUserLog/login',
              header: {
                'content-type': 'application/x-www-form-urlencoded'
              },
              data: {
                appid: 'wx363536b420e01cd1',
                secret: '8bc23bed8f51078504ffc9656606e8c8',
                js_code: code,
                grant_type: 'authorization_code',
              },
              success: function (res2) {
                if (res2.data.status == '0') {
                  wx.setStorageSync('openid', res2.data.data);
                  console.log(" 登录成功");
                  if (that.globalData.userInfo) {
                    if (that.userInfoReadyCallback) {
                      that.userInfoReadyCallback(that.globalData.userInfo);
                      var encryptedData = that.globalData.userInfo.encryptedData;
                      var openid = wx.getStorageSync("openid");
                      var iv = that.globalData.userInfo.iv;
                      wx.request({
                        url: that.globalData.requestURL + 'wxUserLog/getUseuInfo',
                        data: {
                          encryptedData: encryptedData,
                          openid: openid,
                          iv: iv
                        },
                        header: {
                          'content-type': 'application/x-www-form-urlencoded'
                        },
                        success: function (res5) {
                          console.log(res5.data);
                          that.globalData.userInfoData = res5.data;
                          if (that.userInfoDataReadyCallback) {
                            that.userInfoDataReadyCallback(that.globalData.userInfoData);
                          }
                        }
                      })
                    }
                  } else if (!that.globalData.userInfo){
                    wx.getUserInfo({
                      withCredentials: true,
                      success: res3 => {
                        // 可以将 res 发送给后台解码出 unionId
                        that.globalData.userInfo = res3
                        // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
                        // 所以此处加入 callback 以防止这种情况
                        if (that.userInfoReadyCallback) {
                          that.userInfoReadyCallback(that.globalData.userInfo);
                          var encryptedData = that.globalData.userInfo.encryptedData;
                          var openid = wx.getStorageSync("openid");
                          var iv = that.globalData.userInfo.iv;
                          wx.request({
                            url: that.globalData.requestURL + 'wxUserLog/getUseuInfo',
                            data: {
                              encryptedData: encryptedData,
                              openid: openid,
                              iv: iv
                            },
                            header: {
                              'content-type': 'application/x-www-form-urlencoded'
                            },
                            success: function (res5) {
                              console.log(res5.data);
                              that.globalData.userInfoData = res5.data;
                              if (that.userInfoDataReadyCallback) {
                                that.userInfoDataReadyCallback(that.globalData.userInfoData);
                              }
                            }
                          })
                         } //else if (!that.globalData.userInfoData) {
                        //   var encryptedData = that.globalData.userInfo.encryptedData;
                        //   var openid = wx.getStorageSync("openid");
                        //   var iv = that.globalData.userInfo.iv;
                        //   wx.request({
                        //     url: that.globalData.requestURL + 'wxUserLog/getUseuInfo',
                        //     data: {
                        //       encryptedData: encryptedData,
                        //       openid: openid,
                        //       iv: iv
                        //     },
                        //     header: {
                        //       'content-type': 'application/x-www-form-urlencoded'
                        //     },
                        //     success: function (res5) {
                        //       console.log(res5.data);
                        //       that.globalData.userInfoData = res5.data;
                        //       if (that.userInfoDataReadyCallback) {
                        //         that.userInfoDataReadyCallback(that.globalData.userInfoData);
                        //       }
                        //     }
                        //   })
                        // }
                      },
                      fail: fal => {
                        wx.showModal({
                          title: '拒绝授权',
                          content: '部分功能将受影响',
                          showCancel: false
                        })
                      }
                    })
                  }
                }
              }
            })
          }
        })
      }
    })
  },


  globalData: {
    imgResponseUrl: '',
    userInfo: null,
    userInfoData: null,
    requestURL: 'http://192.168.243.11:8070/',
    // requestURL: 'https://api.gcjxglzx.com',
    progress: 0,//上传进度
    totalBytesSent: '',//已经上传的数据长度
    totalBytesExpectedToSend: '',//预期需要上传的数据总长度
    ak: 'D5bM5nzi2RSRKez7Xwn9EOd7m1ZPtGhR',
    newList: [
      {
        id: 1, imgurl: 'https://api.gcjxglzx.com/images/yizhong-wechat/news/new1.jpg',
        title: '雄安新设备', url: '/page-news/news',
        desc: '“绿色拆迁”风向标：看看雄安新区拆迁都用了哪些设备。',
        text: '在雄安新区，大批量的工程机械设备在短短10个小时之内就拆除了8000平米的混凝土框架。最主要的是整个过程它是无噪音、无灰尘的，并且13000吨的建筑垃圾也将100%的循环再利用.可以说这将会是国内未来绿色施工，风向标而这种拥有多种机械组成的施工技术也有一个文艺的名字——“绿色高科技拆除技术”这次绿色拆除工程完全舍弃以往使用的工程爆破、炮头机拆除、挖掘机拆除等手段。取而代之的是液压剪、金刚锯、水压刀、破拆机器人、移动破碎机等高科技手段，实现无噪音、无灰尘绿色拆除。'
      },
      {
        id: 2, imgurl: 'https://api.gcjxglzx.com/images/yizhong-wechat/news/new2.jpg',
        title: '这是第二个新闻', url: '/page-news/news',
        desc: '由各种物质组成的巨型球状天体，叫做星球。星球有一定的形状，有自己的运行轨道。',
        text: ''
      },
      {
        id: 3, imgurl: 'https://api.gcjxglzx.com/images/yizhong-wechat/news/new3.jpg',
        title: '这是第三个新闻', url: '/page-news/news',
        desc: '由各种物质组成的巨型球状天体，叫做星球。星球有一定的形状，有自己的运行轨道。',
        text: ''
      },
      {
        id: 4, imgurl: 'https://api.gcjxglzx.com/images/yizhong-wechat/news/new4.jpg',
        title: '这是第四个新闻', url: '/page-news/news',
        desc: '由各种物质组成的巨型球状天体，叫做星球。星球有一定的形状，有自己的运行轨道。',
        text: ''
      },
      {
        id: 5, imgurl: 'https://api.gcjxglzx.com/images/yizhong-wechat/news/new5.jpg',
        title: '这是第五个新闻', url: '/page-news/news',
        desc: '由各种物质组成的巨型球状天体，叫做星球。星球有一定的形状，有自己的运行轨道。',
        text: ''
      },

    ],
    submitImage: [
      'https://api.gcjxglzx.com/images/yizhong-wechat/legend/sfzqm.jpg',
      'https://api.gcjxglzx.com/images/yizhong-wechat/legend/sfzbm.jpg',
      'https://api.gcjxglzx.com/images/yizhong-wechat/legend/certificate.jpg',
      'https://api.gcjxglzx.com/images/yizhong-wechat/legend/zhengchesfz.jpg',
      'https://api.gcjxglzx.com/images/yizhong-wechat/legend/zhengqianfang.jpg',
      'https://api.gcjxglzx.com/images/yizhong-wechat/legend/zuoqianfang.jpg',
      'https://api.gcjxglzx.com/images/yizhong-wechat/legend/youqianfang.jpg',
      'https://api.gcjxglzx.com/images/yizhong-wechat/legend/youhoufang.jpg',
      'https://api.gcjxglzx.com/images/yizhong-wechat/legend/zuohoufang.jpg',
      'https://api.gcjxglzx.com/images/yizhong-wechat/legend/nameplate.jpg',
      'https://api.gcjxglzx.com/images/yizhong-wechat/legend/engine.jpg',

    ],
    safeImage: [
      'https://api.gcjxglzx.com/images/yizhong-wechat/legend/sfzqm.jpg',
      'https://api.gcjxglzx.com/images/yizhong-wechat/legend/sfzbm.jpg',
      'https://api.gcjxglzx.com/images/yizhong-wechat/legend/certificate.jpg',

      'https://api.gcjxglzx.com/images/yizhong-wechat/legend/zhengchesfz.jpg',
      'https://api.gcjxglzx.com/images/yizhong-wechat/legend/zhengqianfang.jpg',
      'https://api.gcjxglzx.com/images/yizhong-wechat/legend/zuoqianfang.jpg',
      'https://api.gcjxglzx.com/images/yizhong-wechat/legend/youqianfang.jpg',
      'https://api.gcjxglzx.com/images/yizhong-wechat/legend/youhoufang.jpg',

      'https://api.gcjxglzx.com/images/yizhong-wechat/legend/zuohoufang.jpg',
      'https://api.gcjxglzx.com/images/yizhong-wechat/legend/nameplate.jpg',
      'https://api.gcjxglzx.com/images/yizhong-wechat/legend/engine.jpg',

    ],
    operationImage: [
      'https://api.gcjxglzx.com/images/yizhong-wechat/legend/sfzqm.jpg',
      'https://api.gcjxglzx.com/images/yizhong-wechat/legend/sfzbm.jpg',
      'https://api.gcjxglzx.com/images/yizhong-wechat/legend/rentou.jpg',
      'https://api.gcjxglzx.com/images/yizhong-wechat/legend/brhkbzl',
      'https://api.gcjxglzx.com/images/yizhong-wechat/legend/pohkbzl',
      'https://api.gcjxglzx.com/images/yizhong-wechat/legend/jhz',
    ],

    registerImage: [
      'https://api.gcjxglzx.com/images/yizhong-wechat/legend/sfzqm.jpg',
      'https://api.gcjxglzx.com/images/yizhong-wechat/legend/sfzbm.jpg',
      'https://api.gcjxglzx.com/images/yizhong-wechat/legend/sfzqm.jpg',
      'https://api.gcjxglzx.com/images/yizhong-wechat/legend/sfzbm.jpg',
      'https://api.gcjxglzx.com/images/yizhong-wechat/legend/certificate.jpg',
      'https://api.gcjxglzx.com/images/yizhong-wechat/legend/invoice.jpg',
      'https://api.gcjxglzx.com/images/yizhong-wechat/legend/purchaseContract.jpg',
      'https://api.gcjxglzx.com/images/yizhong-wechat/legend/zhengchesfz.jpg',
      'https://api.gcjxglzx.com/images/yizhong-wechat/legend/zhengqianfang.jpg',
      'https://api.gcjxglzx.com/images/yizhong-wechat/legend/zuoqianfang.jpg',
      'https://api.gcjxglzx.com/images/yizhong-wechat/legend/youqianfang.jpg',
      'https://api.gcjxglzx.com/images/yizhong-wechat/legend/youhoufang.jpg',
      'https://api.gcjxglzx.com/images/yizhong-wechat/legend/zuohoufang.jpg',

      'https://api.gcjxglzx.com/images/yizhong-wechat/legend/nameplate.jpg',
      'https://api.gcjxglzx.com/images/yizhong-wechat/legend/dajiazisfz.jpg',
      'https://api.gcjxglzx.com/images/yizhong-wechat/legend/engine.jpg',
      'https://api.gcjxglzx.com/images/yizhong-wechat/legend/yeyabeng.jpg',
    ],
    bg_oneselfIDcardImg:'https://api.gcjxglzx.com/images/yizhong-wechat/legend/bg_oneselfIDcardImg.jpg'
    // sfzqm: submitImage[0],
    // sfzbm: submitImage[],
    // rentou: '/images/rentou.jpg',
    // hegezheng: '/images/hegezheng.jpg',
    // fapiao: '/images/fapiao.jpg',
    // caigouhetong: '/images/caigouhetong.jpg',
    // shouquanzhengshu: '/images/shouquanzhengshu.jpg',
    // zhengchesfz: '/images/zhengchesfz.jpg',
    // zhengqianfang: '/images/zhengqianfang.jpg',
    // zuoqianfang: '/images/zuoqianfang.jpg',
    // youqianfang: '/images/youqianfang.jpg',
    // youhoufang: '/images/youhoufang.jpg',
    // zuohoufang: '/images/zuohoufang.jpg',
    // mingpaisfz: '/images/mingpaisfz.jpg',
    // dajiazisfz: '/images/dajiazisfz.jpg',
    // fadongji: '/images/fadongji.jpg',
    // yeyabeng: '/images/yeyabeng.jpg',
  }
})